package com.lc1993929.mr.rating;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

/**
 * Created by LiuChang on 2017/11/14/014.
 */
public class UserRatingTopN {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        //向mapreduce内部各组件传递参数
        conf.set("user.rate.topn", args[0]);

        //conf.set("mapreduce.framework.name", "local");
        Job job = Job.getInstance(conf);
        //要执行的jar文件
        job.setJarByClass(UserRatingTopN.class);
        //指定mapper类和reducer类
        job.setMapperClass(UserRatingTopNMapper.class);
        job.setReducerClass(UserRatingTopNReducer.class);
        //指定mapper和reducer的输出的key和value的数据类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(RateBean.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(RateBean.class);
        //指定要解析的文件类型
        job.setInputFormatClass(TextInputFormat.class);
        //指定要解析的文件路径
        FileInputFormat.setInputPaths(job, new Path(args[1]));
        //指定要输出的文件类型
        job.setOutputFormatClass(TextOutputFormat.class);
        //指定输出的文件路径
        FileOutputFormat.setOutputPath(job, new Path(args[2]));

        //设置reduce任务个数
        job.setNumReduceTasks(1);
        // 向yarn提交job
        boolean res = job.waitForCompletion(true);

        System.exit(res ? 0 : 1);
    }
}
